cap log close
log using ${log}\01_split_episodes.log, replace


/*
	~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	
	SIAB Preparation
	
	Split episodes that span over one year (only relevant for the sources LeH and LHG)
	
	Modifies the variables:
		- begepi: splitted version of begepi
		- endepi: splitted version of endepi
		- jahr: year
		- age: age, measured in years
		
	Generates the variables:
		- begepi_orig: original version of begepi
		- endepi_orig: original version of endepi
	
	
	Author(s): Wolfgang Dauth, Johann Eppelsheimer
	
	Version: 1.0
	Created: 2018-06-01
	
	~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/



********************************************************************************
* Save original begin and end date of episodes
********************************************************************************
gen int begepi_orig = begepi
gen int endepi_orig = endepi

label var begepi_orig "Episode start date; episodes might be > 1 year"
label var endepi_orig "Episode end date; episodes might be > 1 year"

format begepi_orig endepi_orig %td


********************************************************************************
* Number of calendar years one spell spans over
********************************************************************************
gen byte span_year = year(endepi) - year(begepi) + 1
label var span_year "Number of years spanned by the spell"


********************************************************************************
* n-plicate observations that span over n>1 calendar years
********************************************************************************
expand span_year, gen(is_clone)


********************************************************************************
* Cut the original spell at end of year
********************************************************************************
replace endepi = mdy(12, 31, year(begepi)) if span_year > 1 & is_clone == 0



/*
	Comment:
	
	The following lines exploit the fact that the counter 'spell' has the same
	value for each spell that spans several years and its clones generated by
	the expand command above.
	
*/

********************************************************************************
* Set the begin date of all clones to January 1st and increase the year
********************************************************************************
bysort persnr spell (is_clone): replace begepi = mdy(1, 1, year(begepi[_n-1]) + 1) if is_clone == 1


********************************************************************************
* For all clone spell except the last of the clone group: set the end date to 31.12. 
********************************************************************************
by persnr spell (is_clone): replace endepi = mdy(12, 31, year(endepi[_n-1]) + 1) if is_clone == 1 & _n < _N


********************************************************************************
* Update the variables 'jahr' and age
********************************************************************************
replace jahr = year(begepi)
replace age = jahr - gebjahr


********************************************************************************
* Clean up
********************************************************************************
drop is_clone span_year


cap log close
